﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
using System.Device.Location;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Notification;
using Hangout.Client.UserLocationService;
using Microsoft.Xna.Framework;

namespace Hangout.Client
{
    public partial class MainPage : PhoneApplicationPage
    {
        Core.Location.CurrentLocation location;

        public MeetupService.Meetup CurrentHangout;

        GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
        
        public GeoPosition<GeoCoordinate> MyLocation;

        public MainPage()
        {
            try
            {
                InitializeComponent();
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

            if (Settings.Settings.FirstLaunch)
            {
                    Settings.Settings.FirstLaunch = false;
                    var res = MessageBox.Show(Messages.FirstStartMessage, "Tutorial?", MessageBoxButton.OKCancel);
                    if (res == MessageBoxResult.OK)
                    {
                        ApplicationBar.IsVisible = false;
                        TutorialControl.TutorialCompleted += new EventHandler(TutorialControl_TutorialCompleted);
                        TutorialControl.Visibility = System.Windows.Visibility.Visible;
                        TutorialControl.TutorialVisibility = System.Windows.Visibility.Visible;
                        return;
                    }
                    else
                    {
                        MessageBox.Show("If you want to view tutorial anytime during the app. Please select help menu from the dashboard page");
                    }
            }

            Load();

        }

        void TutorialControl_TutorialCompleted(object sender, EventArgs e)
        {
            TutorialControl.TutorialCompleted-=new EventHandler(TutorialControl_TutorialCompleted);
            TutorialControl.Visibility = System.Windows.Visibility.Collapsed;
            ApplicationBar.IsVisible = true;
            Load();
        }

        private void Load()
        {
            try
            {


                //check of the user is registered
                if (Settings.Settings.UserID == -1) //if not registered, then redirect to account page :)
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        NavigationService.Navigate(new Uri(Navigation.Accounts, UriKind.RelativeOrAbsolute));
                    });

                    return;
                }

                if (!Core.User.User.ValidateUserProfile())
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        NavigationService.Navigate(new Uri(Navigation.Profile, UriKind.RelativeOrAbsolute));
                    });

                    return;
                }



                Core.Startup.Launch.ConnectToPnService();
                Core.Startup.Launch.StartLocationTracking();
                StartGeoWatcher();
                
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void StartGeoWatcher()
        {
            try
            {
                location = new Core.Location.CurrentLocation();
                location.StartWatcher(); //get the location and update that to the DB :)
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        

        void HangoutList_ShoutNewHangoutSelected(object sender, EventArgs e)
        {
            try
            {
                GotoShoutOutPage();
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        

        

       

       

        

       

        
        
        

       
       

       

        

        

        

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                //profile
                NavigateToProfile();
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
            
        }

        private void ApplicationBarMenuItem_Click_1(object sender, EventArgs e)
        {
            try
            {
                //account
                NavigateToAccount();
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void NavigateToProfile()
        {
            try
            {
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Profile, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }



        private void NavigateToAccount()
        {
            try
            {
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Accounts, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.HangoutPinned, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

       

        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            try
            {
                GotoShoutOutPage();
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void GotoShoutOutPage()
        {
            try
            {
                //shout out
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.InitiateHangout, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void HelpBtn(object sender, System.EventArgs e)
        {
            try
            {
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Help, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void AboutBtn(object sender, System.EventArgs e)
        {
            try
            {
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.About, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }

        private void ApplicationBarMenuItem_Click_2(object sender, EventArgs e)
        {
            try
            {
                //settings
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Settings, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }



        #region NotificationControl

        public void AttachEvents()
        {
            try
            {
                NotificationControl.NavigateToHangout += new Controls.Notifications.NotificationReceived.IdHelper(NotificationControl_NavigateToHangout);
                NotificationControl.NavigateToProfile += new Controls.Notifications.NotificationReceived.IdHelper(NotificationControl_NavigateToProfile);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
               
            }
        }

        void NotificationControl_NavigateToProfile(int id)
        {
            try
            {
                NavigateToViewProfile(id);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
               
            }
        }

        void NotificationControl_NavigateToHangout(int id)
        {
            try
            {
                NavigateToHangout(id);
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        private void NavigateToHangout(int id)
        {
            try
            {
                //navigate to hangout page
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Hangout + "?id=" + id, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        private void NavigateToViewProfile(int id)
        {
            try
            {
                //navigate to hangout page
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.ViewProfile + "?id=" + id, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);

            }
        }

        #endregion

        private void ApplicationBarIconButton_Click_3(object sender, EventArgs e)
        {
            try
            {
                //navigate to notifications. :)
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Notifications, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }


        private void SearchBtn_Click(object sender, EventArgs e)
        {
            try
            {
                //navigate to notifications. :)
                Dispatcher.BeginInvoke(() =>
                {
                    NavigationService.Navigate(new Uri(Navigation.Search, UriKind.RelativeOrAbsolute));
                });
            }
            catch (System.Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }

        }


        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            
            base.OnNavigatingFrom(e);
        }


        protected override void  OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
        }


        
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (TutorialControl.Visibility == System.Windows.Visibility.Visible)
            {
                if (TutorialControl.Visibility == System.Windows.Visibility.Visible && TutorialControl.TutorialImage.Visibility == System.Windows.Visibility.Visible)
                {
                    if (TutorialControl.Visibility == System.Windows.Visibility.Visible && TutorialControl.CurrentImage != 0)
                    {
                        e.Cancel = true;
                        if (TutorialControl.CurrentImage > 0)
                            TutorialControl.CurrentImage--;
                    }
                    else
                        if (TutorialControl.CurrentImage == 0)
                        {
                            TutorialControl.TutorialImage.Visibility = System.Windows.Visibility.Collapsed;
                            e.Cancel = true;
                        }
                }
                else
                    if (TutorialControl.Visibility == System.Windows.Visibility.Visible)
                    {
                        e.Cancel = true;
                        
                        TutorialControl.TutorialCompleted -= new EventHandler(TutorialControl_TutorialCompleted);
                        TutorialControl.Visibility = System.Windows.Visibility.Collapsed;
                        ApplicationBar.IsVisible = true;

                        TutorialControl_TutorialCompleted(new object(), new EventArgs());
                        Load();
                    }


                return;
            }
            var res = MessageBox.Show("Do you want to leave the application?", "Exit?", MessageBoxButton.OKCancel);
            if (res == MessageBoxResult.OK)
            {
                new Game().Exit();
            }
            else
            {

            }
            e.Cancel = true;
            base.OnBackKeyPress(e);
        }
       
    }
}